package com.pospi.pospihold.ui.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.mylhyl.circledialog.CircleDialog;
import com.mylhyl.circledialog.params.ProgressParams;
import com.pospi.pospihold.R;
import com.pospi.pospihold.util.LoadingDialog;

import java.util.ArrayList;
import java.util.List;

public class BaseActivity extends SuperBaseActivity {
    private TextView mTitle;
    private ImageView mBack;
    private TextView menu;
    private FrameLayout container;
    private LoadingDialog loadingDialog;
    private static List<Activity> activities = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base);
        mTitle = (TextView) findViewById(R.id.title);
        mBack = (ImageView) findViewById(R.id.back);
        menu = (TextView) findViewById(R.id.menu);
        container = (FrameLayout) findViewById(R.id.container);
        activities.add(this);

    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        activities.remove(this);
    }

    public static void killBaseActivity() {
        for (Activity activity : activities) {
            activity.finish();
        }
    }

    /***
     * 设置内容区域
     *
     * @param resId 资源文件ID
     */
    @SuppressWarnings("deprecation")
    public void setContentLayout(int resId) {

        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View contentView = inflater.inflate(resId, null);
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
                ViewGroup.LayoutParams.FILL_PARENT);
        contentView.setLayoutParams(layoutParams);
        contentView.setBackgroundDrawable(null);
        if (null != container) {
            container.addView(contentView);
        }
    }

    /**
     * 设置中间title的文字
     */
    public void setTitle(String title) {
        mTitle.setText(title);
    }

    /**
     * 设置中间title的文字
     */
    public void setTitle(int title) {
        mTitle.setText(title);
    }

    /**
     * 设置右边按钮的文字
     */
    public void setMenuText(String text) {
        menu.setVisibility(View.VISIBLE);
        menu.setText(text);
    }
    public String getMenuText() {
        return menu.getText().toString().contains("添加餐桌") ? "无" : menu.getText().toString();
    }

    /**
     * 设置右边按钮的图片资源
     */
    public void setMenuImage(int resId) {
        if (null != menu) {
            menu.setVisibility(View.VISIBLE);
            menu.setBackgroundResource(resId);
        }
    }

    /**
     * 设置左边后退按钮的隐藏与显示
     */
    public void setBackVisible(boolean visible) {
        if (visible) {
            mBack.setVisibility(View.VISIBLE);
        } else {
            mBack.setVisibility(View.GONE);
        }
    }

    /**
     * 设置右边按钮的点击事件
     */
    public void onMenuClick(View view) {

    }

    /**
     * 设置左边后退按钮的点击事件
     */
    public void onBack(View view) {
        onBackPressed();
    }

    /**
     * 打开一个Activity
     */
    public void startActivity(Class<?> cls) {
        Intent intent = new Intent(this, cls);
        startActivity(intent);
    }
    /**
     * 设置添加屏幕的背景透明度
     **/
    public void backgroundAlpaha(Activity context, float bgAlpha) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        context.getWindow()
                .addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        context.getWindow().setAttributes(lp);
    }
    private DialogFragment dialog;

    public void showDialog() {
        dialog = new CircleDialog.Builder()
                .setProgressText("请求中...")
                .setProgressStyle(ProgressParams.STYLE_SPINNER)
                .show(getSupportFragmentManager());
    }

    public void close() {
        dialog.dismiss();
    }

    public void showLoading(String msg){
        if (loadingDialog == null) {
            loadingDialog = new LoadingDialog(this);
            loadingDialog.setMessage(msg);
            loadingDialog.show();
        }
    }
    public void closeLoading() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
            loadingDialog = null;
        }
    }

}